package com.hengxinguotong.zhihuichengjian.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.google.gson.Gson;
import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.adapter.HorizontalListViewAdapter;
import com.hengxinguotong.zhihuichengjian.ui.app.BaseFragment;
import com.hengxinguotong.zhihuichengjian.bean.CountAlarm;
import com.hengxinguotong.zhihuichengjian.bean.ProjectBean;
import com.hengxinguotong.zhihuichengjian.ui.ActivityJumper;
import com.hengxinguotong.zhihuichengjian.ui.MainActivity;
import com.hengxinguotong.zhihuichengjian.utils.SPUtil;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.view.HorizontalListView;
import com.lidroid.xutils.http.RequestParams;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/16.
 */

public class SuperviseFragment extends BaseFragment implements AdapterView.OnItemClickListener{

    private MainActivity mainActivity;
    private View rootView = null;
    @BindView(R.id.tower_warn)
    HorizontalListView towerWarn;
    @BindView(R.id.lift_warn)
    HorizontalListView liftWarn;
    @BindView(R.id.envir_warn)
    HorizontalListView envirWarn;

    private int[] warn_number = {1400, 1210, 908, 820, 790, 630, 540, 480};
    private String[] area = {"洪山区", "青山区", "东湖高新区", "武昌区", "江夏区", "江岸区", "汉阳区", "硚口区"};
    private String type ;
    private int userType = 2;//1：工地管理人员；2：市建委用户
    private List<ProjectBean> liftAlarmList, towerAlarmList, pm10AlarmList;
    private HorizontalListViewAdapter adapter;

    public static SuperviseFragment newInstance(String type){
        Bundle bundle = new Bundle();
        bundle.putString("supervise",type);
        SuperviseFragment fragment = new SuperviseFragment();
        fragment.setArguments(bundle);
        return fragment;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_supervise, null);
            ButterKnife.bind(this, rootView);
            mainActivity = (MainActivity) getActivity();


        }else {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null) {
                parent.removeView(rootView);
            }
        }
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    //获取各区所有工地预警次数
    private void  countAlarm(){
        JSONObject jsonObject = new JSONObject();

        try {
            type = getArguments().getString("supervise");
            switch (type){
                case "supervise":
                    userType = 2;
                    break;
                case "project":
                    userType = 1;
                    break;
            }
            jsonObject.put("userType", userType);//1：工地管理人员；2：市建委用户
        } catch (JSONException e) {
            e.printStackTrace();
        }

        RequestParams params = new RequestParams();
        params.addHeader("Content-type", "application/json");
        try {
            params.setBodyEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Utils.requestData(null, mainActivity, "/main/countAlarm/" + SPUtil.getString(mainActivity, "uuid"), params, new Utils.ListenerJson() {
            @Override
            public void onSuccess(String json) {
                if(!Utils.isEmpty(json)){
                    Gson gson = new Gson();
                    CountAlarm countAlarm = gson.fromJson(json, CountAlarm.class);
                    process(countAlarm.getValue());
                }
            }

            @Override
            public void onFailure(String json) {

            }
        });
    }

    private void process(CountAlarm.AlarmValue value){
        if(null != value.getTowerAlarmCount()){
            towerAlarmList = value.getTowerAlarmCount();
            adapter = new HorizontalListViewAdapter(value.getTowerAlarmCount(), mainActivity);
            towerWarn.setAdapter(adapter);
        }else{
            showEmptyView(towerWarn);
        }
        if(null != value.getLiftAlarmCount()){
            liftAlarmList = value.getLiftAlarmCount();
            adapter = new HorizontalListViewAdapter(value.getLiftAlarmCount(), mainActivity);
            liftWarn.setAdapter(adapter);
        }else{
            showEmptyView(liftWarn);
        }
        if(null != value.getPm10AlarmCount()){
            pm10AlarmList = value.getPm10AlarmCount();
            adapter = new HorizontalListViewAdapter(value.getPm10AlarmCount(), mainActivity);
            envirWarn.setAdapter(adapter);
        }else{
            showEmptyView(envirWarn);
        }
    }

//    private void getBeans(){
//        projects = new ArrayList<>();
//        for(int i = 0; i < warn_number.length; i++){
//            ProjectBean bean = new ProjectBean();
//            bean.setAreaName(area[i]);
//            bean.setCount(warn_number[i]);
//            projects.add(bean);
//        }
//    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String url ;
        String uuid = SPUtil.getString(mainActivity, "uuid");
        String areaId ;
        switch (view.getId()){
            case R.id.tower_warn:
                areaId = towerAlarmList.get(position).getAreaId();
                url = "/towercrane/toTowercrane/" + uuid + "?areaId=" + areaId + "&userType=" + userType;
                ActivityJumper.jumpToWebView(url, "二级菜单", mainActivity);
                break;
            case R.id.lift_warn:
                areaId = liftAlarmList.get(position).getAreaId();
                url = "/lift/toLift/" + uuid + "?areaId=" + areaId + "&userType=" + userType;
                ActivityJumper.jumpToWebView(url, "二级菜单", mainActivity);
                break;
            case R.id.envir_warn:
                areaId = pm10AlarmList.get(position).getAreaId();
                url = "/environment/toAlarm/" + uuid + "?areaId=" + areaId + "&userType=" + userType;
                ActivityJumper.jumpToWebView(url, "二级菜单", mainActivity);
                break;
        }
    }

    @Override
    public void onLazyLoad() {
        countAlarm();
    }
}
